package com.zd.lib.http.okhttp;


import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 用于上传文件的进度显示
 */
public class ProgressRequestInterceptor implements Interceptor{
    ProgressListener progressListener ;

    public ProgressRequestInterceptor(ProgressListener progressListener) {
        this.progressListener = progressListener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        if (progressListener == null) {
            return chain.proceed(chain.request());
        }
        return chain.proceed(transformer(chain.request()));
    }


    private Request transformer(Request request) {
        return  request.newBuilder()
                .method(request.method(),
                        new ProgressRequestBody(request.body(), progressListener))
                .build();
    }
}
